package com.weiyuwqj.server.service.sys.impl;

import com.weiyuwqj.api.util.Sms;
import com.weiyuwqj.server.service.base.BaseServiceImpl;
import com.weiyuwqj.server.mapper.*;
import com.weiyuwqj.api.service.NoticePush;
import com.weiyuwqj.api.service.SysAppClientService;
import com.weiyuwqj.api.constant.Constants;
import com.weiyuwqj.api.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by huzj on 2016/9/20.
 */
@Service("appClientService")
public class SysAppClientServiceImpl extends BaseServiceImpl<SysAppClient> implements SysAppClientService {
    @Autowired
    SysAppClientMapper appClientMapper;

    @Autowired
    SysNoticeTypeMapper noticeTypeMapper;

    @Autowired
    BasUserMapper userMapper;

    @Autowired
    SysNoticeMapper noticeMapper;

    @Autowired
    NoticePush noticePush;

    @Autowired
    SysPushLogMapper pushLogMapper;

    @Override
    public void updateOrInsert(Integer userId, String clientId) {
        if (clientId == null) {
            return;
        }
        SysAppClient appClient = appClientMapper.getAppClientByUserId(userId);
        if (appClient != null) {
            //其他设备登录发送通知短信和推送
            if (!clientId.equals(appClient.getClientId())) {
                BasUser user = userMapper.selectByPrimaryKey(userId);
                SysNoticeType smsNoticeType = noticeTypeMapper.getNoticeTypeByNid(Constants.other_equipment_login_sms);
                if (user != null && smsNoticeType != null) {
                    Sms.send(user.getMobile(), smsNoticeType.getTypeContent());
                    SysNotice notice = new SysNotice();
                    notice.setTitle(smsNoticeType.getTitle());
                    notice.setUserId(userId);
                    notice.setStatus(0);
                    notice.setAddtime(new Date());
                    notice.setTypee(smsNoticeType.getType());
                    notice.setReceiveId(user.getMobile());
                    notice.setContents(smsNoticeType.getTypeContent());
                    noticeMapper.insert(notice);
                }
                SysNoticeType pushNoticeType = noticeTypeMapper.getNoticeTypeByNid(Constants.other_equipment_login_push);
                if (user != null && pushNoticeType != null) {
                    noticePush.getOnePush(appClient.getClientId(), pushNoticeType.getTitle(), pushNoticeType.getTypeContent());
                    SysPushLog pushLog = new SysPushLog();
                    pushLog.setTitle(pushNoticeType.getTitle());
                    pushLog.setUserId(userId);
                    pushLog.setStatus(0);
                    pushLog.setAddtime(new Date());
                    pushLog.setType(pushNoticeType.getType());
                    pushLog.setClientId(appClient.getClientId());
                    pushLog.setContents(pushNoticeType.getTypeContent());
                    pushLogMapper.insert(pushLog);
                }
            }
            appClient.setClientId(clientId);
            appClientMapper.updateByPrimaryKeySelective(appClient);
        } else {
            appClient = new SysAppClient();
            appClient.setUserId(userId);
            appClient.setClientId(clientId);
            appClientMapper.insert(appClient);
        }
    }
}
